package com.dick.weixin.custom;

import java.util.List;

import net.sf.json.JSONObject;

import com.dick.weixin.message.resp.Article;
import com.dick.weixin.message.resp.Music;
import com.dick.weixin.util.CommonUtil;
import com.dick.weixin.util.MenuUtil;
import com.dick.weixin.util.logging.Logger;
import com.dick.weixin.util.logging.LoggerFactory;

/**
 * 
 * 类 名 称： CustomMessage 类 描 述： 组装客服消息 创 建 人： zhangyaomin 邮
 * 箱：zhangyaomin@yunchuangyi.cn 创建时间：2014-3-10上午11:23:50
 */
public class CustomMessage {

	private static Logger log = LoggerFactory.getLogger(MenuUtil.class);

	// 获取access_token的接口地址（GET）限200（次/天）
	public final static String access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";

	/**
	 * 组装文本客服消息
	 * 
	 * @param openId
	 *            消息发送对象
	 * @param content
	 *            文本消息内容
	 * @return
	 */
	public static String makeTextCustomMessage(String openId, String content) {

		content = content.replace("\"", "\\\"");
		String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"text\",\"text\":{\"content\":\"%s\"}}";
		return String.format(jsonMsg, openId, content);
	}

	/**
	 * 组装图片客服消息
	 * 
	 * @param openId
	 *            消息发送对象
	 * @param mediaId
	 *            媒体文件ID
	 * @return
	 */
	public static String makeImageCustomMessage(String openId, String mediaId) {
		String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"%s\"}}";
		return String.format(jsonMsg, openId, mediaId);
	}

	/**
	 * 组装语音客服消息
	 * 
	 * @param openId
	 *            消息发送对象
	 * @param mediaId
	 *            媒体文件ID
	 * @return
	 */
	public static String makeVoiceCustomMessage(String openId, String mediaId) {
		String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"%s\"}}";
		return String.format(jsonMsg, openId, mediaId);
	}

	/**
	 * 组装视频客服消息
	 * 
	 * @param openId
	 *            消息发送对象
	 * @param mediaId
	 *            媒体文件ID
	 * @return
	 */
	public static String makeVideoCustomMessage(String openId, String mediaId,
			String thumbMediaId) {
		String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"%s\",\"thumb_media_id\":\"%s\"}}";
		return String.format(jsonMsg, openId, mediaId, thumbMediaId);
	}

	/**
	 * 组装音乐客服消息
	 * 
	 * @param openId
	 *            消息发送对象
	 * @param mediaId
	 *            媒体文件ID
	 * @return
	 */
	public static String makeMusicCustomMessage(String openId, Music music) {
		String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"music\",\"music\":%s}";
		jsonMsg = String.format(jsonMsg, openId, JSONObject.fromObject(music)
				.toString());
		jsonMsg = jsonMsg.replace("thumbmediaid", "thumb_media_id");
		return jsonMsg;
	}

	/**
	 * 组装音乐客服消息
	 * 
	 * @param openId
	 *            消息发送对象
	 * @param mediaId
	 *            媒体文件ID
	 * @return
	 */
	public static String makeNewsCustomMessage(String openId,
			List<Article> articleList) {
		String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"news\",\"news\":{\"articles\"}}";
		jsonMsg = String.format(jsonMsg, openId, JSONObject.fromObject(
				articleList).toString().replaceAll("\"", "\\\""));
		jsonMsg = jsonMsg.replace("thumbmediaid", "thumb_media_id");
		return jsonMsg;
	}

	/**
	 * 发送客服消息
	 * 
	 * @param accessToken
	 *            接口访问凭证
	 * @param jsonMsg
	 *            格式的客服消息（包括touser,msgtype和消息内容）
	 * @return
	 */
	public static boolean sendCustomMessage(String accessToken, String jsonMsg) {

		boolean result = false;
		// 拼接请求地址
		String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";
		requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken);
		// 发送客服消息
		JSONObject jsonObject = CommonUtil.httpRequest(requestUrl, "POST",
				jsonMsg);

		if (null != jsonObject) {
			int errorCode = jsonObject.getInt("errcode");
			String errorMsg = jsonObject.getString("errmsg");
			if (0 == errorCode) {
				result = true;
				log.info("客服消息发送成功 errcode:{} errmsg:{}", errorCode + "",
						errorMsg);
			} else {
				log.error("客服消息发送失败 errcode:{} errmsg:{}", errorCode + "",
						errorMsg);
			}

		}

		return result;
	}

	public static void main(String[] args) {
		String accessToken = CommonUtil.getToken("wxa8deedd53399ac87",
				"1df79d8b41a4c77e92a5f1de8951dc7b").getAccessToken();
		String jsonTextMsg = makeTextCustomMessage("yaodick", "测试");
		sendCustomMessage(accessToken, jsonTextMsg);
	}
}
